// ==========================================================================
// GFast自动生成controller操作代码。
// 生成日期：2024-11-11 20:33:48
// 生成路径: internal/app/system/controller/addresses.go
// 生成人：王中阳
// desc:收货地址
// company:云南奇讯科技有限公司
// ==========================================================================

package controller

import (
	"context"
	"errors"

	"github.com/gogf/gf/v2/encoding/gurl"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/tiger1103/gfast/v3/api/v1/system"
	"github.com/tiger1103/gfast/v3/internal/app/system/model"
	"github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/libUtils"
	"github.com/xuri/excelize/v2"
)

type addressesController struct {
	BaseController
}

var Addresses = new(addressesController)

// List 列表
func (c *addressesController) List(ctx context.Context, req *system.AddressesSearchReq) (res *system.AddressesSearchRes, err error) {
	res = new(system.AddressesSearchRes)
	res.AddressesSearchRes, err = service.Addresses().List(ctx, &req.AddressesSearchReq)
	return
}

// Export 导出excel
func (c *addressesController) Export(ctx context.Context, req *system.AddressesExportReq) (res *system.AddressesExportRes, err error) {
	var (
		r        = ghttp.RequestFromCtx(ctx)
		listData []*model.AddressesInfoRes
		//表头
		tableHead = []interface{}{"", "用户id", "姓名", "手机号", "学校id", "地图选点地址", "详细地址", "粘贴板内容", "经度", "纬度", "是否删除", "是否默认", "", ""}
		excelData [][]interface{}
		//字典选项处理
	)
	req.PageNum = 1
	req.PageSize = 500
	//获取字典数据
	excelData = append(excelData, tableHead)
	for {
		listData, err = service.Addresses().GetExportData(ctx, &req.AddressesSearchReq)
		if err != nil {
			return
		}
		if listData == nil {
			break
		}
		for _, v := range listData {
			var (
				//单选-用户id
				userid string
				//单选-学校id
				schoolId string
			)
			//关联表-单选-用户id
			if v.LinkedUserid != nil {
				userid = v.LinkedUserid.Username
			}
			//关联表-单选-学校id
			if v.LinkedSchoolId != nil {
				schoolId = v.LinkedSchoolId.Schoolname
			}
			dt := []interface{}{
				v.Id,
				//关联表-单选-用户id
				userid,
				v.Name,
				v.Phone,
				//关联表-单选-学校id
				schoolId,
				v.Addr,
				v.Detail,
				v.AddrName,
				v.Lat,
				v.Lng,
				v.IsDel,
				v.IsDefault,
				v.CreatedAt.Format("Y-m-d H:i:s"),
				v.UpdatedAt.Format("Y-m-d H:i:s"),
			}
			excelData = append(excelData, dt)
		}
		req.PageNum++
	}
	//创建excel处理对象
	excel := new(libUtils.ExcelHelper).CreateFile()
	defer excel.Close()
	excel.ArrToExcel("Sheet1", "A1", excelData)
	col, _ := excelize.ColumnNumberToName(len(tableHead))
	row := len(excelData)
	cr, _ := excelize.JoinCellName(col, row)
	excel.SetCellBorder("Sheet1", "A1", cr)
	_, err = excel.WriteTo(r.Response.BufferWriter)
	if err != nil {
		return
	}
	r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	r.Response.Header().Set("Accept-Ranges", "bytes")
	r.Response.Header().Set("Access-Control-Expose-Headers", "*")
	r.Response.Header().Set("Content-Disposition", "attachment; filename="+gurl.Encode("收货地址")+".xlsx")
	r.Response.Buffer()
	r.Exit()
	return
}
func (c *addressesController) ExcelTemplate(ctx context.Context, req *system.AddressesExcelTemplateReq) (res *system.AddressesExcelTemplateRes, err error) {
	var (
		r = ghttp.RequestFromCtx(ctx)
		//表头
		tableHead = []interface{}{"用户id", "姓名", "手机号", "学校id", "地图选点地址", "详细地址", "粘贴板内容", "经度", "纬度", "是否删除", "是否默认", "", ""}
		excelData = [][]interface{}{tableHead}
	)
	//创建excel处理对象
	excel := new(libUtils.ExcelHelper).CreateFile()
	defer excel.Close()
	excel.ArrToExcel("Sheet1", "A1", excelData)
	col, _ := excelize.ColumnNumberToName(len(tableHead))
	row := len(excelData)
	cr, _ := excelize.JoinCellName(col, row)
	excel.SetCellBorder("Sheet1", "A1", cr)
	_, err = excel.WriteTo(r.Response.BufferWriter)
	if err != nil {
		return
	}
	r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	r.Response.Header().Set("Accept-Ranges", "bytes")
	r.Response.Header().Set("Access-Control-Expose-Headers", "*")
	r.Response.Header().Set("Content-Disposition", "attachment; filename="+gurl.Encode("收货地址模板")+".xlsx")
	r.Response.Buffer()
	r.Exit()
	return
}
func (c *addressesController) Import(ctx context.Context, req *system.AddressesImportReq) (res *system.AddressesImportRes, err error) {
	err = service.Addresses().Import(ctx, req.File)
	return
}

// LinkedAddressesDataSearch 相关连表查询数据
func (c *addressesController) LinkedAddressesDataSearch(ctx context.Context, req *system.LinkedAddressesDataSearchReq) (res *system.LinkedAddressesDataSearchRes, err error) {
	if !service.SysUser().AccessRule(ctx, service.Context().GetUserId(ctx), "api/v1/system/addresses/list") {
		err = errors.New("没有访问权限")
		return
	}
	res = new(system.LinkedAddressesDataSearchRes)
	res.LinkedAddressesDataSearchRes, err = service.Addresses().LinkedAddressesDataSearch(ctx)
	return
}

// Get 获取收货地址
func (c *addressesController) Get(ctx context.Context, req *system.AddressesGetReq) (res *system.AddressesGetRes, err error) {
	res = new(system.AddressesGetRes)
	res.AddressesInfoRes, err = service.Addresses().GetById(ctx, req.Id)
	return
}

// Add 添加收货地址
func (c *addressesController) Add(ctx context.Context, req *system.AddressesAddReq) (res *system.AddressesAddRes, err error) {
	err = service.Addresses().Add(ctx, req.AddressesAddReq)
	return
}

// Edit 修改收货地址
func (c *addressesController) Edit(ctx context.Context, req *system.AddressesEditReq) (res *system.AddressesEditRes, err error) {
	err = service.Addresses().Edit(ctx, req.AddressesEditReq)
	return
}

// Delete 删除收货地址
func (c *addressesController) Delete(ctx context.Context, req *system.AddressesDeleteReq) (res *system.AddressesDeleteRes, err error) {
	err = service.Addresses().Delete(ctx, req.Ids)
	return
}
